#include <iostream>
using namespace std;
#include <functional>
#include <string>
#include <unistd.h>

void serviceIo(int sockfd)
{
    char buffer[1024];
    while (1)
    {
        ssize_t n = read(sockfd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "recv message:" << buffer << endl;

            // 然后还要给客户端回复信息
            string outbuffer = buffer;
            outbuffer += "server[echo]";
            write(sockfd, outbuffer.c_str(), sizeof(outbuffer));
        }
        else
        {
            break;
        }
    }
    close(sockfd);
}

class Task
{
public:
    typedef function<void(int)> fun_t;
    Task()
    {
    }
    Task(int sock, fun_t func) : _sock(sock), _func(func)
    {
    }

    // 仿函数：这里的函数是需要用户自己传的
    void operator()()
    {
        _func(_sock);
    }

private:
    int _sock;
    fun_t _func;
};
